import { size, IFormProps, pageSize, dateTimeFormat, dateFormat } from "../../utils/common";
import React from "react";
import { connect } from "dva";
import { Form, Row, Col, Button, Input, Table, Popconfirm, Select } from 'antd';
import { PaginationProps } from "antd/lib/pagination";
import moment from 'moment';
const Styles = require("../../styles/index.less");
import * as api from '../../api/api';

export interface IPointsItemsPros extends IFormProps {
    item: any
}
class PointsItems extends React.PureComponent<IPointsItemsPros, any>{
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        this.getStores();
    }

    search = () => {
        this.getPointsItems();
    }

    getPointsItems = (payload?) => {
        payload = Object.assign({}, this.props.item.pointsItemSearchs, this.props.form.getFieldsValue(), { ...payload });
        this.props.dispatch({
            type: "item/getPointsItems",
            payload
        });
    }

    getStores = () => {
        this.props.dispatch({
            type: "item/getStores",
            payload: {
                callback: () => {
                    this.search();
                }
            }
        });
    }

    reset = () => {
        this.props.form.resetFields();
    }

    edit = (id) => {
        this.props.history.push('/mall/item?id=' + id);
    }

    remove = (id) => {
        this.props.dispatch({
            type: "item/removePointsItem",
            payload: {
                form: { id },
                callback: () => {
                    this.getPointsItems();
                }
            }
        })
    }

    storeChange = (storeId) => {
        this.props.dispatch({
            type: 'item/save',
            payload: {
                storeId
            }
        });
        this.getPointsItems({ storeId });
    }

    render(): JSX.Element {
        //console.log(this.props);
        const { getFieldDecorator } = this.props.form;
        const { loading } = this.props;
        const {
            storeId,
            pointsItems: { totalCount, items },
            stores: { items: stores },
            pointsItemSearchs: { keywords, pageIndex, pageSize, } }
            = this.props.item;

        const pageChange = (pageIndex, pageSize) => {
            let payload = Object.assign({},
                this.props.item.itemSearchs,
                { pageIndex, pageSize });
            this.getPointsItems(payload);
        }
        const pagination: PaginationProps | boolean = {
            total: totalCount,
            defaultCurrent: 1,
            current: pageIndex,
            pageSize: pageSize,
            showSizeChanger: true,
            showTotal: (total) => `共 ${total} 条`,
            showQuickJumper: true,
            pageSizeOptions: ["10", "20", "30", "50"],
            onShowSizeChange: pageChange,
            onChange: pageChange
        };
        return (
            <Row className={Styles.F_BG_WHITE1}>
                <Row className={Styles.F_BG_WHITE}>
                    <Col span={24}>
                        <Form layout="inline">
                            <Form.Item label='请选择店铺'>
                                {getFieldDecorator('storeId', {
                                    initialValue: storeId
                                })(
                                    <Select style={{ width: 120 }} onChange={this.storeChange}>
                                        {
                                            stores.map((store: api.SingleUserMallStoresVOStoreVO) => {
                                                return <Select.Option value={store.id}>{store.name}</Select.Option>
                                            })
                                        }
                                    </Select>)}
                            </Form.Item>
                            <Form.Item label='关键字搜索'>
                                {getFieldDecorator('keywords', {
                                    initialValue: keywords
                                })(<Input size={size}
                                    placeholder="请输入商品名称/标签" />)}
                            </Form.Item>
                            {
                                storeId ? (
                                    <Form.Item>
                                        <Button size={size} type="primary" loading={loading} onClick={this.search}>查询</Button>
                                    </Form.Item>
                                ) : (null)
                            }
                        </Form>
                        <Col span={24}>
                            <hr className={Styles.hrBorder} />
                            <h3>积分商品列表</h3>
                        </Col>
                        <Col span={24}>
                            <Table
                                dataSource={items}
                                bordered
                                pagination={pagination}
                                rowKey={(record: api.SingleUserMallItemsVOPointsItemManageVO) => String(record.id)}
                                loading={loading}
                            >
                                <Table.Column title="序号" key="no" render={(text, record, index) => {
                                    return (pageIndex - 1) * pageSize + index + 1
                                }} />
                                <Table.Column title="名称" key="name" dataIndex="name" />
                                <Table.Column title="图片" key="picture" render={(text, record: api.SingleUserMallItemsVOPointsItemManageVO, index) => {
                                    return <img width='100px' src={record.picture} />
                                }} />
                                <Table.Column title="规格" key="spec" dataIndex="spec" />
                                <Table.Column title="所需积分" key="points" dataIndex="points" />
                                <Table.Column title="上架时间" key="time" render={(text, record: api.SingleUserMallItemsVOPointsItemManageVO, index) => {
                                    return moment(record.creationTime).format(dateTimeFormat)
                                }} />
                                <Table.Column title="操作" render={(text, record: api.SingleUserMallItemsVOPointsItemManageVO, index) => {
                                    return (
                                        <div>
                                            <a className={Styles.M_RIGHT} onClick={() => this.edit(record.itemId)}>
                                                编辑
                                            </a>
                                            <Popconfirm title="确定删除吗?" onConfirm={() => { this.remove(record.id); }}>
                                                <a>删除</a>
                                            </Popconfirm>
                                        </div>
                                    );
                                }} />
                            </Table>
                        </Col>
                    </Col>
                </Row>
            </Row>)
    }
}
export default connect(({ item, loading: { models: { item: loading } } }) => ({ item, loading }))(Form.create()(PointsItems));